public with sharing class CashFlowClass{
	
	public Opportunity[] oppsList{get;set;}
	public Kreditor__c[] currentkreditorList{get;set;}
	public Kreditor__c[] nextkreditorList{get;set;}
	public Kreditor__c[] unpaidkreditorList{get;set;}
	public Kreditor__c objKreditor {get;set;}
	public Debitoren_Rechnung__c[] debitorList{get;set;}
	public Debitoren_Rechnung__c[] unpaiddebitorList{get;set;}
	public OpportunityLineItem [] oppLineItemList{get;set;}
	public double[] bankkontoStand {get;set;}
	public double umsatzsumme1{get;set;}
	public double umsatzsumme2{get;set;}
	public double kostensumme1{get;set;}
	public double kostensumme2{get;set;}
	public double ebitda1{get;set;}
	public double ebitda2{get;set;}
	public double oposkreditoren{get;set;}
	public double oposdebitoren{get;set;}
	public double cashFlow1{get;set;}
	public double cashFlow2{get;set;}
	public Date nextstartDate{get;set;} 
	public Date startDate{get;set;}
	public Date endDate{get;set;}
	public Date nextendDate{get;set;}
	
	
	public CashFlowClass(ApexPages.StandardController stdController){
		objKreditor = (Kreditor__c) stdController.getRecord();
		//EndDate
		objKreditor.Paid__c = date.newInstance(2012, 8, 31);
		//StartDate
		objKreditor.Invoice_date__c =date.newInstance(2012, 8, 1);
		
		//oppsList = new List<Opportunity>();
		oppsList = new List<Opportunity>();
		currentkreditorList = new List<Kreditor__c>();
		
		unpaidkreditorList = new List<Kreditor__c>();
		oppLineItemList = new List<OpportunityLineItem>();
		debitorList = new List<Debitoren_Rechnung__c>();
		unpaiddebitorList = new List<Debitoren_Rechnung__c>();
		
		
		
		startDate = System.today().toStartOfMonth();
		nextstartDate = System.today().toStartOfMonth().addMonths(1);
		endDate = System.today().addMonths(1).toStartOfMonth().addDays(-1);
		nextendDate =System.today().addMonths(2).toStartOfMonth().addDays(-1);
		umsatzsumme1 = 0.0;
		umsatzsumme2 = 0.0;
		kostensumme1 = 0.0;
	 	kostensumme2 = 0.0;
		ebitda1 = 0.0;
		ebitda2 = 0.0;
		oposdebitoren = 0.0;
		oposkreditoren = 0.0;
		cashFlow1 = 0.0;
		cashFlow2 = 0.0;
		
		
		this.fillLists();
		
		kostensumme1=oppsList.size();
		umsatzsumme1=this.oppLineItemList.size();
		ebitda1=umsatzsumme1-kostensumme1;
		
		
	}
	public void fillLists(){
		
		try{
			oppsList = [SELECT Id,Name, CloseDate, StageName FROM Opportunity WHERE StageName=:'Geschlossene und gewonnene'];	
		}catch(QueryException ex){
			ApexPages.addMessages(ex);
					
		}
		try{
			currentkreditorList=[SELECT Id, Invoice_amount__c, Name, Invoice_due_on__c,Opportunity__c, Payment_Status__c FROM Kreditor__c WHERE Opportunity__c IN:oppsList AND Invoice_due_on__c>=:System.today().toStartOfMonth() AND Invoice_due_on__c<=: System.today().addMonths(1).addDays(-1)];
		}catch(QueryException ex){
			ApexPages.addMessages(ex);
					
		}
		try{
			oppLineItemList=[SELECT Id,OpportunityId, UnitPrice, ServiceDate, Quantity, Startdatum__c, Leistung_erbracht__c FROM OpportunityLineItem WHERE OpportunityId IN:oppsList];
		}catch(QueryException ex){
			ApexPages.addMessages(ex);
		}
		try{
			unpaiddebitorList =[SELECT Id, Invoice_Amount_net__c,Name, Due_Date__c, Payment_Status__c, Opportunity__c FROM Debitoren_Rechnung__c WHERE Payment_Status__c =: 'Unpaid' AND Due_Date__c<: System.today().toStartOfMonth()];
		}catch(QueryException ex){
			ApexPages.addMessages(ex);
		}
		try{
			unpaidkreditorList = [SELECT Id, Invoice_amount__c, Name, Invoice_due_on__c,Opportunity__c, Payment_Status__c FROM Kreditor__c WHERE Payment_Status__c ='Unpaid' AND Invoice_due_on__c<:System.today().toStartOfMonth()];
		}catch(QueryException ex){
			ApexPages.addMessages(ex);
		}
	}
	public void aktualisieren(){
		if(oppLineItemList.size()>0){
			for(Integer i=0;i< this.oppLineItemList.size();i++){
				
				if((this.oppLineItemList[i].ServiceDate<=objKreditor.Paid__c) && (this.oppLineItemList[i].ServiceDate>=objKreditor.Invoice_date__c)){
					umsatzsumme1 = umsatzsumme1 + oppLineItemList[i].UnitPrice * oppLineItemList[i].Quantity;
				}
				
			}// End for(Integer i=0;i< this.debitorList.size();i++)
		}//End if(oppLineItemList.size()>0)
		
		if(this.currentkreditorList.size()>0){
			for(Integer i=0;i< this.currentkreditorList.size();i++){
				
				if(this.currentkreditorList[i].Payment_Status__c=='Paid'){
					kostensumme1 = kostensumme1+ currentkreditorList[i].Invoice_amount__c;	
				}
				else if(this.currentkreditorList[i].Payment_Status__c=='Unpaid'){
					
				}
				else if(this.currentkreditorList[i].Payment_Status__c=='Canceled'){
					
				}
			}// End for(Integer i=0;i< this.currentkreditorList.size();i++)
		}//End if(this.currentkreditorList.size()>0
		ebitda1=umsatzsumme1-kostensumme1;
	}// End public void aktualisieren()

}